﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using QuoteTree;
using System.IO;

public partial class Products : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.IsNewSession)
        {
            // If it says it is a new session, but an existing cookie exists, then it must 
            // have timed out (can't use the cookie collection because even on first 
            // request it already contains the cookie (request and response
            // seem to share the collection)
            string szCookieHeader = Request.Headers["Cookie"];
            if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
            {
                Response.Redirect("sessionTimeout.aspx");
            }
        }

        if (this.Request.QueryString["cmp_id"] != null)
        {
            string cmp_id = this.Request.QueryString["cmp_id"].ToString();
            DataSet ds = qman.DataHelp.DataAccess.getDataSet("select [user], company_name from company_accounts where cmp_id='" + cmp_id + "'");
            string company_user_name = ds.Tables[0].Rows[0][0].ToString();
            string company_name = ds.Tables[0].Rows[0][1].ToString();

            //Set session variables
            this.Session["company"] = company_user_name;

            //Read product names from dir
            string[] dirs = Directory.GetDirectories(Server.MapPath("~/Private/Quotes/" + company_user_name));
            string[] temp;
            for (int i = 0; i < dirs.Length; i++)
            {
                temp = dirs[i].Split("\\".ToCharArray());
                dirs[i] = temp[temp.Length - 1];
            }

            foreach (string t in dirs) 
            {
                ListItem item = new ListItem();
                item.Text = t;
                item.Value = "~/Private/TreeView.aspx?cun=" + company_user_name + "&product=" + t ;
                this.BulletedList1.Items.Add(item);
            }
            this.Label1.Text = company_name + "' Products:";


        }

        else
        {
            // De alguna forma listar todos los productos independientemente de la compania.
        }






    }
}
